/*
Copyright 2010 Sieric
http://sieric.ca

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vanilla;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;



@SuppressWarnings("serial")
public class Viewer extends JFrame implements ActionListener {
	private int speedWidth, speedHeight;
	private int wantedWidth, wantedHeight;
	private int nextWidth, nextHeight;
	private JComponent nextComponent = null;
	
	private Timer sizeTimer = new Timer(50, this);
	
	public Viewer() {
		// Set the properties
		setTitle("Savior");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new GridLayout(0, 1));
		setVisible(true);
		setResizable(false);
		
		ToStart();
	}
	
	public void ToStart() {
		// Check if there are projects that can be opened
		if (Profile.ListProfiles().length > 0) {
			new ProfileList(this);
		} else {
			new NewProject(this);
		}		
	}
	
	public void AnimateChangeSize(int _width, int _height) {
		wantedWidth = _width;
		wantedHeight = _height;
		
		speedWidth = (wantedWidth - getWidth()) * sizeTimer.getDelay()/500;
		speedHeight = (wantedHeight - getHeight()) * sizeTimer.getDelay()/500;
		
		sizeTimer.start();
	}
	
	public void AnimateChangeComponent(JComponent _new, int _width, int _height) {
		// Disable the component
		if (getContentPane().getComponentCount() != 0) {
			getComponent(0).setEnabled(false);
		}
		
		// Change component
		nextComponent = _new;
		
		// Change size
		nextWidth = _width;
		nextHeight = _height;
		AnimateChangeSize(getWidth(), getHeight());
	}

	public void actionPerformed(ActionEvent e) {
		// Check if near the end
		if ( (Math.abs((wantedWidth - getWidth())) <= Math.abs(speedWidth)) && (Math.abs((wantedHeight - getHeight())) <= Math.abs(speedHeight))) {
			// Stop
			sizeTimer.stop();
			setSize(wantedWidth, wantedHeight);
			
			// Check if change the page
			if (nextComponent != null) {
				// Change size
				AnimateChangeSize(nextWidth, nextHeight);
				
				// Change component
				getContentPane().removeAll();
				add(nextComponent);
				
				// Don't let it change again
				nextComponent = null;
			}
		} else {
			// Sometimes, it doesn't want to increment, so we need to be harder
			int x = getWidth() + speedWidth;
			int y = getHeight() + speedHeight;
			setSize(x, y);
			if (getWidth() != x) {
				speedWidth *= 2;
			}
			if (getHeight() != y) {
				speedHeight  *= 2;
			}
		}
	}

        public static void main(String[] args) {
            new Viewer();
        }
}